应用程序的目的是使用exoplayer通过URL流播放某些广播电台。遇到的问题是,当应用程序运行且通知启动时,有2种不同的流正在播放,其中一种是通过我从无线电UI按下按钮来播放,另一种是通过通知来播放。
我的问题是如何做到这一点,所以当我按下UI上的按钮时,它也会更改通知播放器上的流。
P.S。我尝试导入一个从mainactivity.java证明的播放器,方法是将其称为静态,但会导致内存泄漏并且流在前台服务中停止。
我从以下位置使用了notificationPlayerManager(exoplayer):https://www.youtube.com/watch?v=svdq1BWl4r8
主要活动类别:
package ----------- etc
import {NgModule} from '@angular/core';
import {RouterModule, Routes} from '@angular/router';
import {InstagramComponent} from './instagram.component';
import {InstagramAnalysisComponent} from './instagram-analysis /instagram-analysis.component';
const instagramRoutes: Routes = [
{
path: '', component: InstagramComponent,
children: [
{path: 'overview', loadChildren: './instagram-overview/io.module#IoModule'},
{path: 'content-production', loadChildren: './instagram-content/instagram-content-production/icp.module#IcpModule'},
{path: 'all-content', loadChildren: './instagram-content/instagram-all-content/iac.module#IacModule'},
{path: 'analyz', component: InstagramAnalysisComponent},
{path: 'create/addig', loadChildren: './add-instagram/add-ig.module#AddIgModule'},
]
}
];
@NgModule({
imports: [
RouterModule.forChild(instagramRoutes)
],
exports: [
RouterModule
]
})
export class InstagramRoutingModule {
}
服务类别:
软件包---等
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.ui.PlayerNotificationManager;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.util.Util;
import org.atsevdev.radioromania.MyServices.*;
import android.view.View;
public class MainActivity extends AppCompatActivity {
public static final String FOREGROUND_SERVICE = "android.permission.FOREGROUND_SERVICE";
//initialising variables
public static final String DigiFm = "http://edge76.rdsnet.ro:84/digifm/digifm.mp3";
public static final String radio_zu = "http://live.romanticfm.ro:9123/radiozu.mp3";
public static final String Radio_popular = "http://mp3.radiopopular.ro:7777/;stream.mp3";
public static final String Eur_fm = "http://89.37.58.103:8000/europafm_mp3_64k";
public static final String radio_rom_acc = "http://stream2.srr.ro:8000/;stream/1";
public static final String PROFMurl = "http://edge126.rdsnet.ro:84/profm/profm.mp3";
public static final String radiobuchuresti = "http://89.238.227.6:8032/\n" +
"Title1=Bucuresti FM\n" +
"Length1=-1\n" +
"Version=2";
public static final String impactfm = "http://89.39.189.159:8000/";
private PlayerView playerView;
public SimpleExoPlayer player;
public boolean clickCheck = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playerView=findViewById(R.id.playview);//playview id xml into private variable
Intent playService = new Intent(this, MyServices.class);
Util.startForegroundService(this, playService);
}
@Override
protected void onStart(){
super.onStart();
player = ExoPlayerFactory.newSimpleInstance(this,
new DefaultTrackSelector());//instantiating new player instances
playerView.setPlayer(player);//xml player to view
//creating datasource
DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(this,
Util.getUserAgent(this, "RadioRomania"));
//extracting stream url
final ExtractorMediaSource mediaSource = new ExtractorMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.parse(PROFMurl));
final ExtractorMediaSource mediaSource0 = new ExtractorMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.parse(radiobuchuresti));
final ExtractorMediaSource mediaSource1 = new ExtractorMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.parse(impactfm));
final ExtractorMediaSource mediaSource2 = new ExtractorMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.parse(radio_rom_acc));
final ExtractorMediaSource mediaSource3 = new ExtractorMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.parse(Eur_fm));
final ExtractorMediaSource mediaSource4 = new ExtractorMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.parse(Radio_popular));
final ExtractorMediaSource mediaSource5 = new ExtractorMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.parse(radio_zu));
final ExtractorMediaSource mediaSource6 = new ExtractorMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.parse(DigiFm));
//start video player
FloatingActionButton stop = (FloatingActionButton) findViewById(R.id.stopPLAY);
FloatingActionButton bt = (FloatingActionButton) findViewById(R.id.profm);
FloatingActionButton bt0 = (FloatingActionButton) findViewById(R.id.rad_buc);//buchuresti
FloatingActionButton bt1 = (FloatingActionButton) findViewById(R.id.impactfm);//impact
FloatingActionButton bt2 = (FloatingActionButton) findViewById(R.id.radio_ro_acc);
FloatingActionButton bt3 = (FloatingActionButton) findViewById(R.id.europafm);
FloatingActionButton bt4 = (FloatingActionButton) findViewById(R.id.radpopular);
FloatingActionButton bt5 = (FloatingActionButton) findViewById(R.id.Radiozuu);
FloatingActionButton bt6 = (FloatingActionButton) findViewById(R.id.DigiFm);
//using onClick methods to choose radio station
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { //ini profm
clickCheck = true;
player.prepare(mediaSource);
player.setPlayWhenReady(clickCheck);
}
});
bt0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { //ini impact
clickCheck = true;
player.prepare(mediaSource0);
player.setPlayWhenReady(clickCheck);
}
});
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){ //ini radio ro acc
clickCheck = true;
player.prepare(mediaSource1);
player.setPlayWhenReady(clickCheck);
}
});
bt2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){ //ini radio ro acc
clickCheck = true;
player.prepare(mediaSource2);
player.setPlayWhenReady(clickCheck);
}
});
bt3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { //ini profm
clickCheck = true;
player.prepare(mediaSource3);
player.setPlayWhenReady(clickCheck);
}
});
bt4.setOnClickListener(new View.OnClickListener() {//ini radio popular
@Override
public void onClick(View v) {
player.prepare(mediaSource4);
}
});
bt5.setOnClickListener(new View.OnClickListener() {//ini radio popular
@Override
public void onClick(View v) {
clickCheck = true;
player.prepare(mediaSource5);
player.setPlayWhenReady(clickCheck);
}
});
bt6.setOnClickListener(new View.OnClickListener() {//ini radio popular
@Override
public void onClick(View v) {
}
});
//stop stream here
stop.setOnClickListener(new View.OnClickListener() {//ini radio popular
@Override
public void onClick(View v) {
player.setPlayWhenReady(false);
}
});
}
@Override
protected void onStop(){
super.onStop();
playerView.setPlayer(null);
player.release();
player = null;
}
答案 0 :(得分:0)
经过研究,我找到了答案。对于其他希望将主要活动绑定到服务的人。
在此过程中,我已使用此方法将播放器与服务绑定:
public SimpleExoPlayer getPlayer1()
{
return player1;
}
然后在主目录中调用它:
player = myServices.player1;