本指南适用于有兴趣在 Android 应用中加载音频广告的发布商。
前提条件
- IMA Android SDK v. 3.16.1 或更高版本。
如果发布商目前没有音频播放器应用,我们建议他们从 GitHub 上的音频播放器示例入手。此示例使用 ExoPlayer 作为前台播放器。本指南的其余部分将介绍播放 IMA 广告的后台音频所需的功能。
在应用中添加后台播放功能
若要在应用处于后台模式时继续播放音乐和音频广告,您可以创建一个包含自定义音频播放器和广告加载器的 Service
组件。
如果您的应用具有用于播放音频的 MainActivity
和 AudioPlayerService
,您可以修改 AndroidManifest.xml
文件,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="…">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<application … >
<activity android:name=".MainActivity" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service android:name=".AudioPlayerService"
android:exported="false"/>
</application>
</manifest>
在此服务中,您可以创建一个自定义音频播放器,该播放器可以是任何媒体播放器,只要还实现了用于与 IMA SDK 交互的 VideoAdPlayer
接口即可。
IMA SDK v. 3.16.1 添加了一个新函数 createAudioAdDisplayContainer
,用于为广告加载器创建专用容器以请求音频广告。
以下代码段展示了用于初始化和发出音频广告请求的 AudioPlayerService
示例。
import android.app.Service;
import com.google.ads.interactivemedia.v3.api.AdDisplayContainer;
import com.google.ads.interactivemedia.v3.api.AdsLoader;
import com.google.ads.interactivemedia.v3.api.AdsRequest;
import com.google.ads.interactivemedia.v3.api.CompanionAdSlot;
import com.google.ads.interactivemedia.v3.api.ImaSdkFactory;
import com.google.ads.interactivemedia.v3.api.ImaSdkSettings;
import com.google.ads.interactivemedia.v3.api.player.VideoAdPlayer;
import com.google.common.collect.ImmutableList;
/** Plays music and ads even when the app enters background mode. */
public class AudioPlayerService extends Service {
/**
* A custom audio player that implements the `VideoAdPlayer` interface.
*/
private VideoAdPlayer audioPlayer;
/**
* An object to create other SDK objects: `AdDisplayContainer`, `AdsLoader` and `AdRequest`.
*/
private ImaSdkFactory sdkFactory;
/**
* An object to make ad requests that would return an `AdsManager`.
*/
private AdsLoader adsLoader;
/**
* An optional list of ViewGroup for rendering companion banners.
*/
private ImmutableList<CompanionAdSlot> companionAdSlots;
/**
* Creates an AdsLoader for requesting ads and handling ad events.
*/
public void initializeAds() {
sdkFactory = ImaSdkFactory.getInstance();
ImaSdkSettings imaSdkSettings = ImaSdkFactory.getInstance().createImaSdkSettings();
AdDisplayContainer container = ImaSdkFactory.createAudioAdDisplayContainer(this, audioPlayer);
if (companionAdSlots != null) {
container.setCompanionSlots(companionAdSlots);
}
adsLoader = sdkFactory.createAdsLoader(this, imaSdkSettings, container);
// Adds event handling logic for ads.
// For more details, see
// https://developers.google.com/interactive-media-ads/docs/sdks/android#create-an-adsloader
adsLoader.addAdsLoadedListener(…);
adsLoader.addAdErrorListener(…);
}
/**
* Adds a companion ad slot. This slot is not required but can be set in ads initialization.
*
* @param companionAdSlot UI container for rendering companion banners.
*/
public void addCompanionAdSlot(CompanionAdSlot companionAdSlot) {
this.companionAdSlots = ImmutableList.of(companionAdSlot);
}
/**
* Makes an audio ad request.
*
* @param adTagUrl Url to download an ad tag.
*/
public void requestAd(String adTagUrl) {
AdsRequest request = sdkFactory.createAdsRequest();
request.setAdTagUrl(adTagUrl);
adsLoader.requestAds(request);
}
...
}
使用包含随播广告位的广告
您可以在有或没有随播广告展示位置的情况下初始化广告。如果您选择呈现随播广告,则需要通过服务 binder 为 MainActivity
类提供对 AudioPlayerService
的访问权限,如下所示:
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import androidx.annotation.Nullable;
...
/** Plays music and ads even when the app enters background mode. */
public class AudioPlayerService extends Service {
...
@Nullable
@Override
public IBinder onBind(Intent intent) {
return new ServiceBinder(this);
}
/**
* Provides access to the `AudioPlayerService` instance after binding.
*/
public class ServiceBinder extends Binder {
private final AudioPlayerService boundService;
/**
* @param service The bound instance of the service
*/
public ServiceBinder(AudioPlayerService service) {
boundService = service;
}
public AudioPlayerService getBoundService() {
return boundService;
}
}
}
在通过 MainActivity
类初始化广告时,您可以调用 addCompanionAdSlot()
函数并传递一个界面容器,用于呈现随播横幅广告。
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.ViewGroup;
import com.google.ads.interactivemedia.v3.api.CompanionAdSlot;
import com.google.ads.interactivemedia.v3.api.ImaSdkFactory;
public class MainActivity extends Activity {
private AudioPlayerService boundService;
private ServiceConnection connection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
ImaSdkFactory sdkFactory = ImaSdkFactory.getInstance();
ViewGroup companionView = findViewById(R.id.companionAdSlotFrame);
final CompanionAdSlot companionAdSlot = sdkFactory.createCompanionAdSlot();
companionAdSlot.setContainer(companionView);
companionAdSlot.setSize(640, 640);
connection =
new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
boundService = ((AudioPlayerService.ServiceBinder) binder).getBoundService();
boundService.addCompanionAdSlot(companionAdSlot);
boundService.initializeAds();
}
...
};
...
bindService(intent, connection, Context.BIND_AUTO_CREATE);
}
...
}
如果您遇到问题,可以将您的实现与音频播放器示例进行比较。如有关于后台音频广告播放的其他问题,请在 IMA SDK 论坛中寻求帮助。