MediaPlayer服务在Android Studio中不起作用

时间:2018-06-27 07:29:37

标签: android android-service android-mediaplayer logcat

我正在将以下代码用于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

活动分类:

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>

有人可以帮助我解决此问题吗?

2 个答案:

答案 0 :(得分:1)

尝试此代码,并在代码中定义以下内容。

// 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" />

答案 1 :(得分:1)

您必须重写onStartCommand()方法并返回START_STICKY,看看这个!

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // play mp3 code here
    return START_STICKY;
}

请参考this link,以更好地理解上述方法和START_STICKY。 希望对您有帮助!