MediaPlayer 服务在 Android Studio 中不工作
MediaPlayer Service not working in Android Studio
我正在为 MediaPlayer
服务使用以下代码。但是下面的服务不是运行。正在触发敬酒,但播放器不播放任何歌曲。
'onStart()' 方法已被弃用,因为我重写了它。
package com.example.user.stringremoval;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.Toast;
public class MyService extends Service {
MediaPlayer mediaPlayer;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate()
{
Toast.makeText(this, "Service Created", Toast.LENGTH_SHORT).show();
mediaPlayer=MediaPlayer.create(this,R.raw.bet);
mediaPlayer.setVolume(50,50);
mediaPlayer.setLooping(false);
}
@Override
public void onStart(Intent intent,int startId)
{
Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
mediaPlayer.start();
}
@Override
public void onDestroy()
{
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_SHORT).show();
mediaPlayer.stop();
}
}
Error in Logcat:
06-27 12:47:03.175 3022-3022/com.example.user.stringremoval E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
Activity class:
package com.example.user.stringremoval;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class AudioPlayer extends AppCompatActivity implements View.OnClickListener {
Button bn1,bn2,bn3;
Intent intent1=getIntent();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio_player);
bn1=(Button)findViewById(R.id.button3);
bn2=(Button)findViewById(R.id.button4);
bn3=(Button)findViewById(R.id.button5);
bn1.setOnClickListener(this);
bn2.setOnClickListener(this);
bn3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.button3:
startService(new Intent(this,MyService.class));
break;
case R.id.button4:
stopService(new Intent(this,MyService.class));
break;
case R.id.button5:
Intent intent=new Intent(this,MainActivity.class);
startActivity(intent);
}
}
}
Android 清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.stringremoval">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AudioPlayer"></activity>
<service android:name=".MyService"
android:enabled="true"/>
</application>
</manifest>
谁能帮我解决这个问题?
尝试使用此代码并将以下内容定义到您的代码中..
// this line define into activity class into oncrateview method.
startService(new Intent(MyActivity.this, MyService.class));
在 android 清单文件中添加以下行 ..
在应用程序标签之间..
<service android:enabled="true" android:name="com.my.packagename.MyService" />
您必须覆盖 onStartCommand()
方法并且 return START_STICKY
看看这个!
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// play mp3 code here
return START_STICKY;
}
参考this link以更好地理解上述方法和START_STICKY
。
希望对您有所帮助!
我正在为 MediaPlayer
服务使用以下代码。但是下面的服务不是运行。正在触发敬酒,但播放器不播放任何歌曲。
'onStart()' 方法已被弃用,因为我重写了它。
package com.example.user.stringremoval;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.Toast;
public class MyService extends Service {
MediaPlayer mediaPlayer;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate()
{
Toast.makeText(this, "Service Created", Toast.LENGTH_SHORT).show();
mediaPlayer=MediaPlayer.create(this,R.raw.bet);
mediaPlayer.setVolume(50,50);
mediaPlayer.setLooping(false);
}
@Override
public void onStart(Intent intent,int startId)
{
Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
mediaPlayer.start();
}
@Override
public void onDestroy()
{
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_SHORT).show();
mediaPlayer.stop();
}
}
Error in Logcat:
06-27 12:47:03.175 3022-3022/com.example.user.stringremoval E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
Activity class:
package com.example.user.stringremoval;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class AudioPlayer extends AppCompatActivity implements View.OnClickListener {
Button bn1,bn2,bn3;
Intent intent1=getIntent();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio_player);
bn1=(Button)findViewById(R.id.button3);
bn2=(Button)findViewById(R.id.button4);
bn3=(Button)findViewById(R.id.button5);
bn1.setOnClickListener(this);
bn2.setOnClickListener(this);
bn3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.button3:
startService(new Intent(this,MyService.class));
break;
case R.id.button4:
stopService(new Intent(this,MyService.class));
break;
case R.id.button5:
Intent intent=new Intent(this,MainActivity.class);
startActivity(intent);
}
}
}
Android 清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.stringremoval">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AudioPlayer"></activity>
<service android:name=".MyService"
android:enabled="true"/>
</application>
</manifest>
谁能帮我解决这个问题?
尝试使用此代码并将以下内容定义到您的代码中..
// this line define into activity class into oncrateview method.
startService(new Intent(MyActivity.this, MyService.class));
在 android 清单文件中添加以下行 .. 在应用程序标签之间..
<service android:enabled="true" android:name="com.my.packagename.MyService" />
您必须覆盖 onStartCommand()
方法并且 return START_STICKY
看看这个!
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// play mp3 code here
return START_STICKY;
}
参考this link以更好地理解上述方法和START_STICKY
。
希望对您有所帮助!