简体   繁体   English

声音(MediaPlayer)在模拟器上运行,但在实际的android设备上却无法运行

[英]Sound(MediaPlayer) working on emulator but not on a real android device

The Emulator I am using is Galaxy Nexus API 22, and my background music(mp3) works perfectly on it and running as a service. 我使用的模拟器是Galaxy Nexus API 22,我的背景音乐(mp3)可以完美运行并作为服务运行。 However, it does not work on my SUMSUNG Galaxy Note, which has API 16, I don't think it is a API-related problem though. 但是,它在具有API 16的SUMSUNG Galaxy Note上不起作用,但是我认为这不是与API相关的问题。

Am i need to write some <uses-permission .../> in Manifest? 我需要在清单中写一些<uses-permission .../>吗? or other solutions. 或其他解决方案。

Thank you in advance!! 先感谢您!!

Here is the problem 这是问题所在

Context context = getApplicationContext();
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
if (!taskInfo.isEmpty()) {
    ComponentName topActivity = taskInfo.get(0).topActivity;
    if (!topActivity.getPackageName().equals(context.getPackageName())) {
        SoundService.pauseSound();
        Toast.makeText(WelcomePage.this, "YOU LEFT YOUR APP", Toast.LENGTH_SHORT).show();
    }
    else {
        Toast.makeText(WelcomePage.this, "YOU SWITCHED ACTIVITIES WITHIN YOUR APP", Toast.LENGTH_SHORT).show();
    }
}

I used these code in the onPause() in WelcomePage Activity (which is the 'MainActivity' in my App) in order to pause the background music when I press the HOME key or whatever.But I found the Toast sometimes immediately shows "YOU LEFT YOUR APP" when this Activity shows up without do anything, so I think these piece of code is where the problem is. 我在WelcomePage Activity(在我的应用程序中为“ MainActivity” onPause()onPause()中使用了这些代码,以便在按HOME键或其他功能时暂停背景音乐。但是我发现Toast有时会立即显示"YOU LEFT YOUR APP"在此活动出现时却没有执行任何操作,因此我认为这些代码就是问题所在。 Can anyone help me? 谁能帮我? Thank you!! 谢谢!!

Try this code, its works here: 试试这个代码,它的工作在这里:

In activity, Declare the media player session before the oncreate like this; 在活动中,像这样在oncreate之前声明媒体播放器会话;

MediaPlayer mp = new MediaPlayer();

Put the mp3 file on res/raw folder! 将mp3文件放在res / raw文件夹中!

Finaly put this code on your java! 最后,将此代码放到您的Java上! (If you want this play automatic put on onCreate, or you can put this in a button if you want). (如果您希望此播放自动放在onCreate上,或者您可以将其放在按钮中)。

mp.stop(); // Stop this if run in another place
mp = MediaPlayer.create(this, R.raw.nameofyourfile);//Load your file from folder
mp.setLooping(false); // Set loop is off, if you want it's on change (true)
mp.start(); // start playing your sound

I hope to help you! 希望对您有所帮助!

Whats the error, can you copy here the log. 错误是什么,您可以在此处复制日志吗? Maybe its a problem with the path to the mp3. 也许这是mp3路径的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在仿真器上工作但不在真正的Android设备上 - Working on Emulator but not on the real Android device MediaPlayer可在模拟器上运行,但不能在android中的实际设备上运行 - MediaPlayer working on emulator, but not on actual device in android MediaPlayer在模拟器上运行,但不在设备上运行 - MediaPlayer working on emulator, but not on device Android应用程序在模拟器上工作,但不在真实设备上 - Android app working on emulator but not on real device Android Studio 既不是模拟器也不是真实设备工作 - Android Studio neither Emulator not Real device working Android SQLiteDatabase与Emulator一起使用,但不能在Real Device上使用 - Android SQLiteDatabase working with Emulator but not on Real Device Android应用程序可在模拟器上运行,但不能在真实设备上运行,SharePreferences - Android app working on emulator but not real device, SharePreferences SharedPreferences 适用于 android 仿真器,但不适用于真实设备 - SharedPreferences working on android emulator but not on real device Android 应用程序在真实设备上崩溃但在模拟器上工作 - Android app crashing on real device but working on emulator 广播接收器在模拟器上工作但不在真实设备上工作(Android Oreo) - Broadcast receiver working on emulator but not on working real device (Android Oreo)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM