- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在构建一个由通过 BLE 接收的消息控制的 Android 音乐播放器。我的应用程序在 API 4.4(18) 上运行良好,但在 8.1(27) 中崩溃可能是由于线程处理不当。这是我的扫描 Activity 和音乐播放器 Activity 以及来自 LogCat 的错误
扫描 Activity
public class MainActivity extends Activity {
private BluetoothAdapter mBluetoothAdapter;
private static final int REQUEST_ENABLE_BT = 1;
private static final long SCAN_PERIOD = 3000;
private Dialog mDialog;
public static final int permconst=7;
public static List<BluetoothDevice> mDevices = new ArrayList<BluetoothDevice>();
public static MainActivity instance = null;
private View mPermissionRationale;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.main);
checkperms();
if (!getPackageManager().hasSystemFeature(
PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, "Ble not supported", Toast.LENGTH_SHORT)
.show();
finish();
}
final BluetoothManager mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = mBluetoothManager.getAdapter();
if (mBluetoothAdapter == null) {
Toast.makeText(this, "Ble not supported", Toast.LENGTH_SHORT)
.show();
finish();
return;
}
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(
BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
ImageButton btn = (ImageButton) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
scanLeDevice();
showRoundProcessDialog(MainActivity.this, R.layout.loading_process_dialog_anim);
Timer mTimer = new Timer();
mTimer.schedule(new TimerTask() {
@Override
public void run() {
Intent deviceListIntent = new Intent(getApplicationContext(),
Device.class);
startActivity(deviceListIntent);
mDialog.dismiss();
}
}, SCAN_PERIOD);
}
});
//scanLeDevice();
showRoundProcessDialog(MainActivity.this, R.layout.loading_process_dialog_anim);
Timer mTimer = new Timer();
mTimer.schedule(new TimerTask() {
@Override
public void run() {
Intent deviceListIntent = new Intent(getApplicationContext(),
Device.class);
startActivity(deviceListIntent);
mDialog.dismiss();
}
}, SCAN_PERIOD);
instance = this;
}
public void showRoundProcessDialog(Context mContext, int layout) {
DialogInterface.OnKeyListener keyListener = new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_HOME
|| keyCode == KeyEvent.KEYCODE_SEARCH) {
return true;
}
return false;
}
};
mDialog = new AlertDialog.Builder(mContext).create();
mDialog.setOnKeyListener(keyListener);
mDialog.show();
// 娉ㄦ��姝ゅ��瑕���惧��show涔���� ������浼���ュ��甯�
mDialog.setContentView(layout);
}
private void scanLeDevice() {
//checkperms();
// new Thread() {
final Handler handler =new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// if(Build.VERSION.SDK_INT>21){
// mBluetoothAdapter.getBluetoothLeScanner().startScan(mleScanCallback);}
mBluetoothAdapter.startLeScan(mLeScanCallback);
}
},4000);
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, final int rssi,
byte[] scanRecord) {
runOnUiThread(new Runnable() {
// new Thread(){
@Override
public void run() {
if (device != null) {
if (mDevices.indexOf(device) == -1)
mDevices.add(device);
}
}
});
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// User chose not to enable Bluetooth.
if (requestCode == REQUEST_ENABLE_BT
&& resultCode == Activity.RESULT_CANCELED) {
finish();
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onDestroy() {
super.onDestroy();
//scanLeDevice(false);
mDevices.clear();
System.exit(0);
}
private void checkperms(){
if(Build.VERSION.SDK_INT>19) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "permissions not granted", Toast.LENGTH_SHORT)
.show();
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_COARSE_LOCATION))
Toast.makeText(this, "pls grant permissions", Toast.LENGTH_SHORT)
.show();
else {
// No explanation needed; request the permission
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
permconst);
return;}
}}}}
音乐播放器:
public int[] bname=new int[]{R.drawable.playinv, R.drawable.pausex, R.drawable.pauseinv, R.drawable.playx,
R.drawable.nextinv, R.drawable.nextx, R.drawable.previousinv, R.drawable.previousx, R.drawable.vdowninv,
R.drawable.vdownx, R.drawable.vupinv, R.drawable.vupx};
private Map<UUID, BluetoothGattCharacteristic> map = new HashMap<UUID, BluetoothGattCharacteristic>();
private final ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName,
IBinder service) {
mBluetoothLeService = ((RBLService.LocalBinder) service)
.getService();
if (!mBluetoothLeService.initialize()) {
Log.e(TAG, "Unable to initialize Bluetooth");
finish();
}
// Automatically connects to the device upon successful start-up
// initialization.
final Handler handler =new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mBluetoothLeService.connect(mDeviceAddress);
}
},2000);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
mBluetoothLeService = null;
}
};
private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (RBLService.ACTION_GATT_DISCONNECTED.equals(action)) {
} else if (RBLService.ACTION_GATT_SERVICES_DISCOVERED
.equals(action)) {
getGattService(mBluetoothLeService.getSupportedGattService());
} else if (RBLService.ACTION_DATA_AVAILABLE.equals(action)) {
displayData(intent.getByteArrayExtra(RBLService.EXTRA_DATA));
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.music_player);
checkperms();
vup= findViewById(R.id.vup);
vdown=findViewById(R.id.vdown);
next =findViewById(R.id.next);
previous= findViewById(R.id.previous);
playpause =findViewById(R.id.plpau);
info=findViewById(R.id.textView);
tload=findViewById(R.id.load);
audioManager=(AudioManager)getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
Toast.makeText(getApplicationContext(),"Android Version: "+Build.VERSION.SDK_INT, Toast.LENGTH_LONG).show();
// mediaPlayer = MediaPlayer.create(getApplicationContext(), tracks[current]);
tload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getSongs();
MUSIC_LOADED=1;
info.setText(songT[current]);
Uri trackUri = ContentUris.withAppendedId(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,songIDS[current]);
mediaPlayer=MediaPlayer.create(getApplicationContext(), trackUri);
}
});
playpause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (pp) {
mediaPlayer.start();
playpause.setBackgroundResource(R.drawable.pausex);
pp = FALSE;
} else {
mediaPlayer.pause();
playpause.setBackgroundResource(R.drawable.playx);
pp = TRUE;}
}});
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
nextTrack();
} });
previous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
previousTrack();
} });
vdown.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
volumeDown(audioManager);
}});
vup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
volumeUp(audioManager); }});
Intent intent = getIntent();
mDeviceAddress = intent.getStringExtra(Device.EXTRA_DEVICE_ADDRESS);
mDeviceName = intent.getStringExtra(Device.EXTRA_DEVICE_NAME);
// getActionBar().setTitle(mDeviceName);
// getActionBar().setDisplayHomeAsUpEnabled(true);
Intent gattServiceIntent = new Intent(this, RBLService.class);
bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
}
@Override
protected void onResume() {
super.onResume();
registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
//mediaPlayer.stop();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
mBluetoothLeService.disconnect();
mBluetoothLeService.close();
// mediaPlayer.stop();
System.exit(0);
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onStop() {
super.onStop();
unregisterReceiver(mGattUpdateReceiver);
//mediaPlayer.stop();
}
@Override
protected void onDestroy() {
super.onDestroy();
mBluetoothLeService.disconnect();
mBluetoothLeService.close();
//mediaPlayer.stop();
System.exit(8);
}
private void displayData(byte[] byteArray) {
if (MUSIC_LOADED==1){
if (byteArray != null) {
String data = new String(byteArray);
x= data.substring (data.length()-1);
//Toast.makeText(getApplicationContext(),x,Toast.LENGTH_SHORT).show();
if (x.equals("1")){
selector=10;
colorChange(vup,selector);
Toast.makeText(getApplicationContext(),"Infitex_keypress_001"+"\n"+ "Volume up",Toast.LENGTH_SHORT).show();
volumeUp(audioManager);
}
if (x.equals("3")){
selector=4;
colorChange(next,selector);
Toast.makeText(getApplicationContext(),"Infitex_keypress_003"+"\n"+ "Track Advance",Toast.LENGTH_SHORT).show();
nextTrack();
}
if (x.equals("5") ){
if (pp){
selector=0;} else selector= 2;
colorChange(playpause,selector);
Toast.makeText(getApplicationContext(),"Infitex_keypress_005"+"\n"+ "Play/Pause",Toast.LENGTH_SHORT).show();
playPause(playpause);}
if(x.equals("7")){
selector=6;
colorChange(previous,selector);
Toast.makeText(getApplicationContext(),"Infitex_keypress_007"+"\n"+ "Track Reverse",Toast.LENGTH_SHORT).show();
previousTrack();
}
if(x.equals("9")){
selector=8;
colorChange(vdown,selector);
Toast.makeText(getApplicationContext(),"Infitex_keypress_009"+"\n"+ "Volume Down",Toast.LENGTH_SHORT).show();
volumeDown(audioManager);
}
}}}
private void getGattService(BluetoothGattService gattService) {
if (gattService == null)
return;
BluetoothGattCharacteristic characteristic = gattService
.getCharacteristic(RBLService.UUID_BLE_SHIELD_TX);
map.put(characteristic.getUuid(), characteristic);
BluetoothGattCharacteristic characteristicRx = gattService
.getCharacteristic(RBLService.UUID_BLE_SHIELD_RX);
mBluetoothLeService.setCharacteristicNotification(characteristicRx,
true);
mBluetoothLeService.readCharacteristic(characteristicRx);
}
private static IntentFilter makeGattUpdateIntentFilter() {
final IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(RBLService.ACTION_GATT_CONNECTED);
intentFilter.addAction(RBLService.ACTION_GATT_DISCONNECTED);
intentFilter.addAction(RBLService.ACTION_GATT_SERVICES_DISCOVERED);
intentFilter.addAction(RBLService.ACTION_DATA_AVAILABLE);
return intentFilter;
}
void nextTrack(){
mediaPlayer.pause();
current+=1;
if (current==5){
current=0;
}
getURI();
info.setText(songT[current]);
playpause.setBackgroundResource(R.drawable.pausex);
mediaPlayer.start();
pp=FALSE;
}
void previousTrack(){
mediaPlayer.pause();
if (current==0){
current=4;}
else {
current -= 1;
}
getURI();
info.setText(songT[current]);
playpause.setBackgroundResource(R.drawable.pausex);
mediaPlayer.start();
pp=FALSE;
}
public void playPause(ImageButton imageButton){
if (pp) {
mediaPlayer.start();
imageButton.setBackgroundResource(R.drawable.pausex);
pp = FALSE;
} else if(pp==FALSE) {
mediaPlayer.pause();
imageButton.setBackgroundResource(R.drawable.playx);
pp = TRUE;
}
}
public void volumeDown(AudioManager audioManager){
audioManager.adjustVolume(AudioManager.ADJUST_LOWER,AudioManager.FLAG_PLAY_SOUND);
}
public void volumeUp(AudioManager audioManager){
audioManager.adjustVolume(AudioManager.ADJUST_RAISE,AudioManager.FLAG_PLAY_SOUND);
}
public void colorChange(final ImageButton imageButton, final int name){
final int last= name+1;
new CountDownTimer(1500, 1000) {
public void onTick(long millisUntilFinished) {
imageButton.setBackgroundResource(bname[name]);
}
public void onFinish() {
imageButton.setBackgroundResource(bname[last]);
}
}.start();
}
// public void bCheck(ImageButton imageButton){
public void getSongs(){
int i=0;
ContentResolver contentResolver=getContentResolver();
Uri uri= EXTERNAL_CONTENT_URI;
Cursor songcursor= contentResolver.query(uri,null,null,null,null);
if (songcursor!=null && songcursor.moveToFirst()) {
int songtitle=songcursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
int songID=songcursor.getColumnIndex(MediaStore.Audio.Media._ID);
do{
songT[i] = songcursor.getString(songtitle);
songIDS[i]=songcursor.getLong(songID);
i+=1;
}
while(songcursor.moveToNext());
songcursor.close();
}}
public void getURI(){
Uri trackUri = ContentUris.withAppendedId(
android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,songIDS[current]);
mediaPlayer=MediaPlayer.create(getApplicationContext(), trackUri);
}
private void checkperms(){
//Toast.makeText(this,"version", Toast.LENGTH_LONG);
if(Build.VERSION.SDK_INT>19) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
Toast.makeText(getApplicationContext(), "permissions not granted", Toast.LENGTH_SHORT)
.show();
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.READ_EXTERNAL_STORAGE))
Toast.makeText(getApplicationContext(),"pls grant permissions", Toast.LENGTH_SHORT)
.show();
else {
// No explanation needed; request the permission
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
MainActivity.permconst);
}
}}}
和 logcat:
com.example.parlatas.messages I/AndroidRuntime: VM exiting with result code 0, cleanup skipped.
I/WindowManager: WIN DEATH: Window{9297a52 u0 com.example.parlatas.messages/com.example.parlatas.messages.MainActivity}I/ActivityManager: Process com.example.parlatas.messages (pid 8530) has died: fore TOP W/ActivityManager:强制删除 ActivityRecord{b15576d u0 com.example.parlatas.messages/.MusicPlayer t245}:app 已死,无保存状态 W/InputDispatcher: channel 'd7b3552 com.example.parlatas.messages/com.example.parlatas.messages.MusicPlayer (server)' ~ 消费者关闭输入 channel 或发生错误。事件=0x9 E/InputDispatcher: channel 'd7b3552 com.example.parlatas.messages/com.example.parlatas.messages.MusicPlayer (server)' ~ Channel 已不可恢复地损坏,将被处理掉! I/WindowManager: WIN DEATH: Window{d7b3552 u0 com.example.parlatas.messages/com.example.parlatas.messages.MusicPlayer} W/InputDispatcher:试图注销已经注销的输入 channel “d7b3552 com.example.parlatas.messages/com.example.parlatas.messages.MusicPlayer(服务器)” W/NotificationService: 对象死于试图隐藏通知 android.app.ITransientNotification$Stub$Proxy@c10b00c 在包 com.example.parlatas.messages
我有单独的类,其中包含 BLE 常量和由扫描 Activity (主 Activity )填充的蓝牙设备列表
最佳答案
如果您的目标 API 级别高于 22,请尝试在 list 中添加以下权限。如果您要使用蓝牙硬件,新版本需要粗略定位权限(访问蜂窝位置信息)。它没有意义,但它需要新的 API 级别。
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-feature android:name="android.hardware.bluetooth_le"
那么要么您必须授予权限,要么应该以较低的 API 级别为目标。
关于android - 通过 BLE 的音乐播放器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50785288/
谁能详细说明以下问题? 蓝牙堆栈如何处理音频数据? 如何处理音频命令? 我们需要任何服务来处理音频数据吗? 提前致谢。 最佳答案 基本上,通过 BLE 的语音命令需要: 一些用于减少所需带宽的音频编解
我正在使用Player库以实现全屏视频播放。我相信它在幕后使用 AVFoundation。 我可以使用Float(self.player.maximumDuration)来实现视频的完整持续时间。但是
我正在制作一种宏记录器/播放器 我已经使用 java.awt.Robot() 等实用程序完成播放器部分,它模拟基本的人类鼠标/键盘输出命令,读取 XML 文件。 我卡在了必须记录该 XML 文件的部分
我目前有以下代码可以在页面上播放 youtube 视频。 //Load player api asynchronously. var tag = document.createElement('s
我需要提供音频内容(但不是音乐,更像是播客;人类语音),我正在考虑使用基于 Flash 的播放器让用户无需下载即可收听内容。 我需要一个免费的可嵌入 Flash 的 mp3 播放器。有什么建议? 因为
html5 player/api 更新了吗?事件 SC.Widget.Events.PLAY, SC.Widget.Events.PAUSE, SC.Widget.Events.FINISH, htm
我想在 Lubuntu VMware 中自动打开和关闭 vlc 播放器。我试过一个shell脚本代码,如: vlc rtmp://code sleep(5) exit 0 or vl
我有一个只支持纵向模式的应用程序,它有一个表格,每个单元格包含一个标题和一个带有 YouTube 视频的 web View 。 现在您将如何让 Youtube 播放器同时处于横向和纵向模式?
我正在尝试在我的应用程序中使用 YouTube 播放器 API,但我不知道如何确定视频是否为直播。如果有人知道如何获得视频的真实持续时间。 更新: 我想出了一种方法来确定内容是否是实时的,我使用我的后
我想创建一个能够播放 YouTube 视频的音频并将下载的内容保存在本地缓存中的应用程序,因此当用户决定恢复或再次播放视频时,它不必再次下载部分视频而只需下载剩余部分(用户可以决定如何处理缓存,以及如
我希望我的页面将 div 显示为模态,然后播放 YouTube 视频。我能够按预期播放视频(下面的代码),但是当我在过滤操作时切换到隐藏的 div 时,页面加载时隐藏的 div 不会将 data-sr
我正在尝试使用 AngularJS 和 WP API 构建 SPA。我使用部分在 ng-view 中加载我需要通过路由显示的所有内容。在此基础上,我添加了 Plangular,它是一个使用 Sound
我找到了一个不错的 HTML 5 音频播放器,它带有基于 plyr 的播放列表和艺术品。它在我的桌面浏览器上运行良好,但在我的移动设备 (iOS) 上,按播放后无法播放。有一个codepen来演示:
我正在尝试通过pyglet在Python 3中播放歌曲。我可以播放和停止播放一首歌曲,但是当我尝试播放下一首歌曲时会产生错误。 I followed these instructions.我将在tki
如何将嵌入的 Vimeo 视频重置为播放完毕后的加载状态? Vimeo API 提供了卸载方法 player.api("unload") 但它不适用于非 Flash 播放器。 最佳答案 使用Vimeo
我有一个用于音频录制和播放的网络应用程序。为此,我正在使用 html5 播放器。 现在我必须开发 Phonegap Android 应用程序。我已将插件(org.apache.cordova.medi
有人知道如何像 SuperFlix 一样将自己的字幕加载到 Netflix 播放器吗?关于 Netflix HTML5 播放器的信息很少,其中之一是我应该可以使用 操作播放器 netflix.cadm
如何将新的黑色 YouTube 播放器嵌入到我的网站(刚刚推出的网站)中? 我以前曾问过这个问题,但它已关闭,因为在投票否决和关闭之前没有人愿意真正阅读该问题。不,我没有问如何嵌入V2或V3播放器,我
几个小时以来,我一直在尝试添加一种打开我的 mp3 文件的方法并在队列中一一打开它们。但我不知道该怎么做。当涉及到单个文件时,我打开并播放不是问题。所以我正在考虑 Media(JavaFX) 类中的线
我知道这个函数 (setFullscreen) 只适用于 HTML5,但它对我不起作用。这是我使用的方式: setFullscreen: true 我希望 JW Player 在页面加载后立即以全屏模
我是一名优秀的程序员,十分优秀!