问题描述
我正在使用Flutter构建音乐播放应用程序.作为一个开发人员,它可以帮助我为两个平台构建一次应用程序.但是,在这两个平台上我都遇到了一些小问题.尽管我已经克服了一些障碍,但是我还是想不出一个主要的错误/问题,而这个错误/问题只是出于制作音乐应用程序的目的.如果应用程序不在前台,则音乐不会改变.
I am building a music playing app using Flutter. It helps me as a single developer to build an app once for both platforms. However there are a few hiccups I have hit on both platforms respectivly. Although I have overcome some of these road bumps, I just can't figure out a major bug/issue that just takes the purpose out of making a music app. Music doesn't change if the app is not in the foreground.
我正在使用以下软件包:audioplayers( https://pub.dartlang.org/packages/audioplayers ).我的应用程序正在播放歌曲,mp3文件是在线托管的,并且具有单独的链接.
I am using the package: audioplayers (https://pub.dartlang.org/packages/audioplayers). My app is streaming songs, the mp3 files are hosted online and have individual links.
当第一首歌曲完成播放时,AudioPlayerState.COMPLETED.我打了电话,播放新的歌曲网址.如果应用程序在前景中,则效果很好,但如果应用程序在背景中,则效果不佳.这是在最新版本的iOS上发生的,而我在Android 5.0(而不是Android 8.0+)上发现了这一点.当我对此进行测试时,在Android Studio的运行"标签中,它确实显示了已进行呼叫,但没有播放歌曲,但是当我打开应用程序时,它确实显示了更新的专辑封面,该专辑封面也位于url上(不在歌曲元数据).如果我叫履历表,则从后台打开应用程序后,歌曲就会开始播放.
When the first song completes playback, AudioPlayerState.COMPLETED. I make a call to play, with the new songs url. This works fine if the app is in the foreground but it doesn't work if the app is in the background. This happens on the latest version of iOS and I have caught this on Android 5.0 (not on Android 8.0+). In the RUN tab of Android Studio when I test this, it does show me the call is made, but song doesn't play however when I open app it does show updated album art, which is located on a url also (not in the song metadata). If I call resume, after opening app back up from the background, song starts to play.
我不知道这是软件包的问题还是iOS的问题,我已经在Github软件包上打开了一个问题.但是,我认为,如果在我检查Xcode的背景音频之前将应用程序最小化,那么它在iOS上就不会播放歌曲了.
I don't know if this is an issue with the package or just how it is with iOS, I have opened an issue on the package Github. However I believe it is something with iOS as before it wasn't playing the song if the app was minimized until I check the background audio in Xcode.
import 'package:flutter/material.dart';
import 'package:audioplayers/audioplayers.dart';
import 'dart:math';
AudioPlayer audioPlayer = new AudioPlayer();
class MusicPlay {
MusicPlay() {
_initAudioPlayer();
}
play(String audioURL) async {
int result = await audioPlayer.play(audioURL);
if (result == 1) {
// success
}
}
nextSong() async {
play(String nextAudioURL)
}
void _initAudioPlayer() {
audioPlayer.audioPlayerStateChangeHandler = (AudioPlayerState state) {
switch (state) {
case AudioPlayerState.PLAYING:
break;
case AudioPlayerState.PAUSED:
break;
case AudioPlayerState.STOPPED:
break;
case AudioPlayerState.COMPLETED:
nextSong();
break;
}
};
}
}
推荐答案
解决方案是iOS平台上的一个解决方案.
Solution is a one liner on the iOS Side.
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
https://stackoverflow.com/a/9721032/6024667
雨燕3 UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
雨燕4 UIApplication.shared.beginReceivingRemoteControlEvents()
在返回之前,将此代码添加到iOS/Runner目录内的AppDelegate.m/AppDelegate.swift中.
Add this code to AppDelegate.m/AppDelegate.swift inside your iOS/Runner directory, just before the return.
这篇关于在后台,iOS和Android中使音乐应用程序保持活动状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!